package com.ccsc.auth.cas.utils;

import com.ccsc.auth.cas.entity.ResourceEntity;
import com.ccsc.auth.cas.entity.RoleEntity;
import com.ccsc.auth.cas.entity.SystemEntity;
import com.ccsc.auth.cas.entity.UserEntity;
import com.ccsc.auth.cas.entity.vo.Resource;
import com.ccsc.auth.cas.entity.vo.Role;
import com.ccsc.auth.cas.entity.vo.System;
import com.ccsc.auth.cas.entity.vo.User;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by xingwen on 2017/2/25.
 */
public class EntityUtil {
    public static User convEntity2User(UserEntity userEntity){
        if(userEntity == null) return null;
        User user = new User();
        try {
            user.setAccount(userEntity.getUserAccount());
            user.setUserId(userEntity.getUserId());
            user.setMail(userEntity.getUserMail());
            user.setPhone(userEntity.getUserPhone());
            user.setSex(userEntity.getUserSex());
            user.setUserName(userEntity.getUserName());
            user.setStatus(userEntity.getUserStatus());
            user.setRoles(convEntity2Role(userEntity.getRoles()));
            user.setSystems(convEntity2System(userEntity.getSystems()));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return user;
        }
        return user;
    }

    private static Role convEntity2Role(RoleEntity roleEntity){
        if(roleEntity == null) return null;
        Role role = new Role();
        try {
            role.setRoleId(roleEntity.getRoleId());
            role.setRoleName(roleEntity.getRoleName());
            role.setRoleDescription(roleEntity.getRoleDescription());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return role;
        }
        return role;
    }

    private static List<Role> convEntity2Role(List<RoleEntity> roleEntityList){
        List<Role> roleList = Lists.newArrayList();
        if(roleEntityList!=null && roleEntityList.size()>0){
            for(RoleEntity roleEntity : roleEntityList){
                Role role = convEntity2Role(roleEntity);
                roleList.add(role);
            }
        }

        Collections.sort(roleList,new Comparator<Role>(){
            public int compare(Role arg0, Role arg1) {
                return arg0.getRoleId() - arg1.getRoleId();
            }
        });
        return roleList;
    }

    private static System convEntity2System(SystemEntity systemEntity){
        if(systemEntity == null) return null;
        System system = new System();
        try {
           system.setSystemId(systemEntity.getSystemId());
           system.setSystemName(systemEntity.getSystemName());
           system.setSystemIdentifier(systemEntity.getSystemIdentifier());
           system.setSystemDescription(systemEntity.getSystemDescription());
           system.setSystemUrl(systemEntity.getSystemUrl());
           system.setSortSeq(systemEntity.getSortSeq());
           system.setResources(convEntity2Resource(systemEntity.getResources()));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return system;
        }
        return system;
    }

    private static List<System> convEntity2System(List<SystemEntity> systemEntityList){
        List<System> systemList = Lists.newArrayList();
        if(systemEntityList!=null && systemEntityList.size()>0){
            for(SystemEntity systemEntity : systemEntityList){
                System system = convEntity2System(systemEntity);
                systemList.add(system);
            }
        }

        Collections.sort(systemList,new Comparator<System>(){
            public int compare(System arg0, System arg1) {
                return arg0.getSystemId() - arg1.getSystemId();
            }
        });
        return systemList;
    }

    private static Resource convEntity2Resource(ResourceEntity resourceEntity){
        if(resourceEntity == null) return null;
        Resource resource = new Resource();
        try {
            resource.setResourceId(resourceEntity.getResourceId());
            resource.setResourceContent(resourceEntity.getResourceContent());
            resource.setResourceDescription(resourceEntity.getResourceDescription());
            resource.setResourceParentId(resourceEntity.getResourceParentid());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return resource;
        }
        return resource;
    }

    private static List<Resource> convEntity2Resource(List<ResourceEntity> resourceEntityList){
        List<Resource> resourceList = Lists.newArrayList();
        if(resourceEntityList!=null && resourceEntityList.size()>0){
            for(ResourceEntity resourceEntity : resourceEntityList){
                Resource resource = convEntity2Resource(resourceEntity);
                resourceList.add(resource);
            }
        }

        Collections.sort(resourceList,new Comparator<Resource>(){
            public int compare(Resource arg0, Resource arg1) {
                return arg0.getResourceId() - arg1.getResourceId();
            }
        });
        return resourceList;
    }
}
